/**
 * 
 */
package com.laozhao.EasySales.business;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;

import com.laozhao.EasySales.model.entity.SysConfig;
import com.laozhao.EasySales.model.entity.SysSql;
import com.laozhao.dreamframe.dbmanager.DBFactory;
import com.laozhao.dreamframe.dbmanager.DataBaseManager;
import com.laozhao.dreamframe.model.ModelUtil;
import com.laozhao.dreamframe.sqlmap.BaseInsert;
import com.laozhao.dreamframe.sqlmap.BaseSelector;
import com.laozhao.dreamframe.sqlmap.Criteria;
import com.laozhao.dreamframe.util.FrameFactory;

/**
 * @author laozhao
 *
 */
public class MasterServer extends AbstractServer {

	SqlServer sqlServer = (SqlServer) FrameFactory.getInstance(SqlServer.class);
	/**
	 * 
	 */
	public MasterServer() {
		super.init();
	}
	
	public List<SysSql> searchSqls(List<Criteria> select,String orderby){
		DataBaseManager dmmgr = DBFactory.getInstance();
		List<SysSql> list = null;
		try {
			Connection conn = dmmgr.openConnect();
			conn.setAutoCommit(false);
			Statement statement = conn.createStatement();
			BaseSelector sel = new BaseSelector(sqlServer.getSqlValue("SEL_ENTITY_SQL_LOAD"),select,orderby);
			String sql= sel.toSql();
			if (sql !=null) {
				ResultSet rs = statement.executeQuery(sql);
				list = ModelUtil.Relection(rs, SysSql.class);
			}
			statement.close();
			conn.commit();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public SysConfig load(){
		DataBaseManager dmmgr = DBFactory.getInstance();
		SysConfig sysConfig = null;
		List<SysConfig> list =  null;
		try {
			Connection conn = dmmgr.openConnect();
			conn.setAutoCommit(false);
			Statement statement = conn.createStatement();
			SysSql sql= sqlServer.getSql("SEL_ENTITY_CONFIG_LOAD");
			if (sql !=null) {

				ResultSet rs = statement.executeQuery(sql.getSqlValue());
				list = ModelUtil.Relection(rs, SysConfig.class);
			}
			if (list != null && list.size() > 0) {
				sysConfig = list.get(0);
			}
			conn.commit();
			dmmgr.closeConnection();
			return sysConfig;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	

	public void insertSql(HashMap<String, Object> map){
//		SysConfig sysConfig = null;
//		List<SysConfig> list =  null;
		try {
			Connection conn = DBFactory.getInstance().openConnect();
			conn.setAutoCommit(false);
			Statement statement = conn.createStatement();
			BaseInsert sel = new BaseInsert("insert INTO sys_sql ( $columnset ) values ( $valueset )",map);
			String sql= sel.toSql();
			System.out.println(sql);
			if (sql !=null) {
				statement.execute(sql);
//				list = ModelUtil.Relection(rs, SysSql.class);
			}
			conn.commit();
//			return sysConfig;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//			return null;
		}
	}
}
